import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import 'commont/diaButton.dart';
import 'commont/input.dart';

class PasswordChange extends BaseDialog {
  void Function(String, String, String)? onChange;
  String oldPassword;
  PasswordChange({super.key, required this.oldPassword});

  @override
  BaseDialogState createState() => _PasswordChangeState();
}

class _PasswordChangeState extends BaseDialogState<PasswordChange> {
  final _old_textControlller = TextEditingController();
  final _new_textControlller = TextEditingController();
  final _second_textControlller = TextEditingController();
  @override
  void initState() {
    super.initState();
    _old_textControlller.text = '';
    _old_textControlller.addListener(() {
      String text = _old_textControlller.text;
    });

    _new_textControlller.text = '';
    _new_textControlller.addListener(() {
      String text = _new_textControlller.text;
    });

    _second_textControlller.text = '';
    _second_textControlller.addListener(() {
      String text = _second_textControlller.text;
    });
  }

  void function(String text) async {
    FocusScope.of(context).unfocus();

    switch (text) {
      case 'OK':
        if (_old_textControlller.text == '') {
          EasyLoading.showInfo('The old password can`t be null');
          return;
        } else if (_old_textControlller.text != widget.oldPassword) {
          EasyLoading.showInfo('The old password is incorrect');
          return;
        } else if (_new_textControlller.text == '') {
          EasyLoading.showInfo('The new password can`t be null');
          return;
        } else if (_second_textControlller.text != _new_textControlller.text) {
          EasyLoading.showInfo('The password doesn`t match twice');
          return;
        }
        Navigator.pop(context);
        if (widget.onChange != null) {
          widget.onChange!(_old_textControlller.text, _new_textControlller.text, _second_textControlller.text);
        }
      case 'Cancel':
        Navigator.pop(context);

      default:
    }
  }

  Widget _get_input(double width, double height, String text, TextEditingController controller) {
    return SizedBox(
      width: width,
      height: height,
      child: Row(
        children: [
          Expanded(
              flex: 4,
              child: Text(
                text,
                textAlign: TextAlign.right,
                style: const TextStyle(fontSize: 13, color: Colors.black),
              )),
          Expanded(
            flex: 5,
            child: InputView(controller: controller),
          ),
        ],
      ),
    );
  }

  @override
  Widget buildContent(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constrains) {
      return Container(
        height: 170,
        child: Stack(
          children: [
            ///标题
            const Positioned(
                left: 10,
                top: 10,
                height: 40,
                child: Text(
                  'Please set your new password',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 14, color: Colors.black),
                )),

            Positioned(
              left: 10,
              right: 10,
              top: 40,
              height: 30,
              child: _get_input(constrains.maxWidth - 20, 30, 'Old password:', _old_textControlller),
            ),
            Positioned(
              left: 10,
              top: 70,
              height: 30,
              child: _get_input(constrains.maxWidth - 20, 30, 'New password:', _new_textControlller),
            ),
            Positioned(
              left: 10,
              top: 100,
              height: 30,
              child: _get_input(constrains.maxWidth - 20, 30, 'Please enter again:', _second_textControlller),
            ),
            Positioned(
                right: 0,
                top: 130,
                height: 40,
                width: 60,
                child: Diabutton(title: 'OK', onClick: () => function('OK'))),

            Positioned(
                right: 60,
                top: 130,
                height: 40,
                width: 70,
                child: Diabutton(title: 'Cancel', onClick: () => function('Cancel'))),
          ],
        ),
      );
    });
  }
}
